﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using ComputerStoreManager;
using Modules.Core;
using Modules.Library.Entities;

namespace ComputerStoreManager.ViewModel {
    public class AddProductViewModel : BaseProductControlViewModel {

        #region Variables
        private TextBox txtManufacturerName;
        private TextBox txtNote;
        private TextBox txtProductId;
        private TextBox txtProductName;
        private TextBox txtProviderName;
        private TextBox txtPurchasePrice;
        private TextBox txtRemaining;
        private TextBox txtSalePrice;
        private ComboBox cbbTypeName;
        private Button btnAddPicture;
        private Button btnAddProduct;
        private Button btnClear;
        #endregion

        #region Constructor
        public AddProductViewModel(TextBox txtManufacturerName, TextBox txtNote, TextBox txtProductId, TextBox txtProductName, TextBox txtProviderName,
            TextBox txtPurchasePrice, TextBox txtRemaining, TextBox txtSalePrice, ComboBox cbbTypeName, Button btnAddPicture, Button btnAddProduct, Button btnClear) :base(txtProductId, txtManufacturerName, txtProviderName,
        txtProductName, txtPurchasePrice, txtSalePrice, txtRemaining, txtNote){
            
            // TODO: Complete member initialization
            this.txtManufacturerName = txtManufacturerName;
            this.txtNote = txtNote;
            this.txtProductId = txtProductId;
            this.txtProductName = txtProductName;
            this.txtProviderName = txtProviderName;
            this.txtPurchasePrice = txtPurchasePrice;
            this.txtRemaining = txtRemaining;
            this.txtSalePrice = txtSalePrice;
            this.cbbTypeName = cbbTypeName;
            this.btnAddPicture = btnAddPicture;
            this.btnAddProduct = btnAddProduct;
            this.btnClear = btnClear;

            InitCmd();
            InitControlInfo();
        }
        #endregion

        #region ICommand
        public ICommand AddProductCommand {
            get;
            private set;
        }

        public ICommand AddPictureCommand {
            get;
            private set;
        }

        public ICommand ClearCommand {
            get;
            private set;
        }
        #endregion

        #region Init

        private void InitCmd() {
            AddProductCommand = new ExecutableCommand(DoAddProduct);
            AddPictureCommand = new ExecutableCommand(DoAddPicture);
            ClearCommand = new ExecutableCommand(DoClearFields);
        }

        public override void InitControlInfo() {
            txtProductId.Text = Converter.ConvertToId(Model.GetThongSoPhu(KindOfObject.BY_PRODUCT));

            DataTable data = Model.GetInitInfo(KindOfObject.BY_PRODUCT);

            foreach(DataRow row in data.Rows) {
                ComboBoxItem item = new ComboBoxItem();
                item.Content = (string)row["TenLoaiMH"];
                item.Tag = (string)row["MaLoaiMH"];
                cbbTypeName.Items.Add(item);
            }

            cbbTypeName.SelectedIndex = 0;

        }

        #endregion

        #region Methods

        public override void DoAddProduct() {
            try
            {

                if (CheckIsNotNull())
                {
                    Product product = new Product();
                    //product.AnhDaiDien
                    //product.ChiTietLinhKien
                    product.GhiChu = txtNote.Text;
                    product.GiaBan = txtSalePrice.Text;
                    product.GiaMua = txtPurchasePrice.Text;
                    product.MaLoaiMatHang = ((ComboBoxItem)cbbTypeName.SelectedItem).Tag.ToString();
                    product.MaMatHang = txtProductId.Text;
                    product.NhaCungCap = txtProviderName.Text;
                    product.NhaSanXuat = txtManufacturerName.Text;
                    product.SoLuongTonKho = Convert.ToInt32(txtRemaining.Text);
                    product.TenMatHang = txtProductName.Text;

                    if (!Model.InsertNewData(product, KindOfObject.BY_PRODUCT))
                    {
                        MessageBox.Show(Model.Error);
                    }
                    else
                    {
                        DoClearFields();
                        //txtProductId.Text = Converter.ConvertToId(Model.GetThongSoPhu(KindOfObject.BY_PRODUCT));
                        MessageBox.Show("Thành công");
                    }
                }
                else
                {
                    MessageBox.Show("Bạn không được bỏ trống bất cứ trường nào", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi thao tác " + ex.Message, "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        private void DoAddPicture() {
            //chua xay dung
        }

        public override void DoClearFields() {
            try
            {
                base.DoClearFields();
                txtProductId.Text = Converter.ConvertToId(Model.GetThongSoPhu(KindOfObject.BY_PRODUCT));

            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi thao tác " + ex.Message, "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        public override bool CheckIsNotNull()
        {
            return base.CheckIsNotNull();
        }

        #endregion
    }
}
